package com.itye.deom1.view;

import com.itye.deom1.Group;
import com.itye.deom1.Student;
import com.itye.deom1.until.Constant;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class StudentListPanel extends JPanel {
    String[] headers = {"学号", "姓名", "小组"};
    JTable studentTable;
    JTextField txtId = new JTextField();
    JTextField txtName = new JTextField();
    JComboBox<String> cmbGroup = new JComboBox<>();
    JButton btnEdit = new JButton("修改");
    JButton btnDelete = new JButton("删除");
    DefaultTableModel tableModel;

    public StudentListPanel() {
        this.setBorder(new TitledBorder(new EtchedBorder(), "学生列表"));
        this.setLayout(new BorderLayout());

        // 准备表格数据
        String[][] data = new String[6][3];
        data[0] = new String[]{"202301", "张三", "一组"};
        data[1] = new String[]{"202302", "李四", "一组"};
        data[2] = new String[]{"202303", "王五", "一组"};
        data[3] = new String[]{"202304", "赵六", "二组"};
        data[4] = new String[]{"202305", "孙七", "二组"};
        data[5] = new String[]{"202306", "周八", "二组"};

        // 创建表格模型和表格
        tableModel = new DefaultTableModel(data, headers);
        studentTable = new JTable(tableModel) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        studentTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(studentTable);
        this.add(scrollPane, BorderLayout.CENTER);

        // 创建底部面板
        JPanel btnPanel = new JPanel();
        btnPanel.add(new JLabel("学号:"));
        btnPanel.add(txtId);
        txtId.setPreferredSize(new Dimension(100, 30));
        
        btnPanel.add(new JLabel("姓名:"));
        btnPanel.add(txtName);
        txtName.setPreferredSize(new Dimension(200, 30));
        
        btnPanel.add(new JLabel("小组:"));
        btnPanel.add(cmbGroup);
        cmbGroup.setPreferredSize(new Dimension(100, 30));
        
        // 初始化小组下拉框
        cmbGroup.addItem("请选择小组");
        cmbGroup.addItem("一组");
        cmbGroup.addItem("二组");

        btnPanel.add(btnEdit);
        btnPanel.add(btnDelete);
        this.add(btnPanel, BorderLayout.SOUTH);

        // 表格选择监听器
        studentTable.getSelectionModel().addListSelectionListener(e -> {
            int selectedRow = studentTable.getSelectedRow();
            if (selectedRow >= 0) {
                txtId.setText((String) tableModel.getValueAt(selectedRow, 0));
                txtName.setText((String) tableModel.getValueAt(selectedRow, 1));
                cmbGroup.setSelectedItem(tableModel.getValueAt(selectedRow, 2));
            }
        });

        // 修改按钮监听器
        btnEdit.addActionListener(e -> {
            int selectedRow = studentTable.getSelectedRow();
            if (selectedRow < 0) {
                JOptionPane.showMessageDialog(this, "请先选择学生", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (txtId.getText() == null || txtId.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请填写学号", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (txtName.getText() == null || txtName.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请填写姓名", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (cmbGroup.getSelectedIndex() == 0) {
                JOptionPane.showMessageDialog(this, "请选择小组", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            // 更新表格数据
            tableModel.setValueAt(txtId.getText(), selectedRow, 0);
            tableModel.setValueAt(txtName.getText(), selectedRow, 1);
            tableModel.setValueAt(cmbGroup.getSelectedItem(), selectedRow, 2);
            
            JOptionPane.showMessageDialog(this, "修改成功", "", JOptionPane.INFORMATION_MESSAGE);
        });

        // 删除按钮监听器
        btnDelete.addActionListener(e -> {
            int selectedRow = studentTable.getSelectedRow();
            if (selectedRow < 0) {
                JOptionPane.showMessageDialog(this, "请先选择学生", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (JOptionPane.showConfirmDialog(this, "删除学生会删除他的考勤、成绩等，确认删除？", "", JOptionPane.YES_NO_OPTION) != 0) {
                return;
            }

            // 从表格中删除
            tableModel.removeRow(selectedRow);
            
            JOptionPane.showMessageDialog(this, "删除学生成功", "", JOptionPane.INFORMATION_MESSAGE);
        });
    }
}